package com.qigao.mall.web.controller.app.merchant;

import com.qigao.mall.api.NoticeService;
import com.qigao.mall.commons.dto.NoticeCreateDto;
import com.qigao.mall.commons.dto.NoticeModiDto;
import com.qigao.mall.commons.dto.NoticeQueryDto;
import com.qigao.mall.commons.dto.NoticeResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.enums.StatusEnum;
import com.qigao.mall.web.controller.app.user.AppNoticeController;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.app.NoticeCreateVo;
import com.qigao.mall.web.vo.app.NoticeModiVo;
import com.qigao.mall.web.vo.app.NoticeQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author luiz
 * @Title: com.qigao.mall.web.controller.app
 * @ProjectName three-mall
 * @date 2019-11-2019/11/5 16:17
 * @Description:
 */
@RestController
@RequestMapping("/app/merchant/notice")
@Api(tags = "APP商户端公告",value = "APP商户端公告API")
public class MerNoticeController extends AppNoticeController {
    private static Logger logger= LoggerFactory.getLogger(MerNoticeController.class);


    @Resource
    private NoticeService noticeService;
    //1.公告查询列表
    @PostMapping("findNoticeList")
    public Result<List<NoticeResultDto>> findNoticeList(@RequestBody NoticeQueryVo noticeQueryVo){
        try{
            NoticeQueryDto noticeQueryDto=new NoticeQueryDto();
            BeanUtils.copyProperties(noticeQueryVo,noticeQueryDto);
            noticeQueryDto.setNoticeStatus(StatusEnum.VALID.getCode());
            return noticeService.findNoticeList(noticeQueryDto);
        }catch (Exception e){
            logger.error("公告查询列表发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //1.新增公告信息
    @PostMapping("/createNotice")
    @ApiOperation(value = "新增公告信息", notes = "新增公告信息")
    public Result<String> createNotice(@RequestBody NoticeCreateVo noticeCreateVo, HttpServletRequest request){
        try{
            //参数校验
            ValidatorUtil.validateEntity(noticeCreateVo,NoticeCreateVo.class);
            NoticeCreateDto noticeCreateDto=new NoticeCreateDto();
            BeanUtils.copyProperties(noticeCreateVo,noticeCreateDto);
            noticeCreateDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            noticeService.createNotice(noticeCreateDto);
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("[新增公告信息]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //3.修改公告
    @PostMapping("/modiNotice")
    @ApiOperation(value = "修改公告信息", notes = "修改公告信息")
    public Result<String> modiNotice(@RequestBody NoticeModiVo noticeModiVo, HttpServletRequest request){
        try{
            //参数校验
            ValidatorUtil.validateEntity(noticeModiVo,NoticeModiVo.class);
            NoticeModiDto noticeModiDto=new NoticeModiDto();
            BeanUtils.copyProperties(noticeModiVo,noticeModiDto);
            noticeModiDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            noticeService.modiNotice(noticeModiDto);
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("[新增公告信息]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //4.删除公告
    @GetMapping("deleteById/{noticeId}")
    public Result deleteById(@PathVariable("noticeId")String noticeId){
        try{
            return noticeService.deleteById(noticeId);
        }catch (Exception e){
            logger.error("公告详情发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

}
